package com.thingple.app.io;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.thingple.eye.BaseActivity;
import com.thingple.json.JsonConvertor;
import com.thingple.rest.RestClient;
import com.thingple.shared_context.SharedContext;

import java.util.HashMap;
import java.util.Map;

public class ConfigActivity extends BaseActivity {

    private static String TAG = "Config";

    private String host = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);
    }

    public void onChangeHost(View view) {

        final Handler handler = new Handler();
        final Context context = this;
        final EditText text = new EditText(this);
        text.setText(host);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.config_host)
        .setView(text)
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                host = text.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            Map<String, String> params = new HashMap();
                            params.put("t", System.currentTimeMillis() + "");
                            String text = RestClient.newInstance().get(host, params);
                            Map map = JsonConvertor.convert2Object(text, Map.class);
                            SharedContext sharedContext = SharedContext.shareInstance();
                            for (Object object : map.keySet()) {
                                String key = "" + object;
                                Object val = map.get(object);

                                if (val != null && !val.toString().trim().equals("")) {
                                    sharedContext.set(object + "", map.get(object) + "");
                                    Log.d(TAG, key + ":" + val);
                                }
                            }
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(context,"Configuration changed", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (Exception e) {
                            Log.e(TAG, e.getMessage(), e);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(context,"Configuration change failed", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    }
                }).start();
                dialog.dismiss();
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })

        .show();
    }
}
